/*
 * SimplePubSub from https://github.com/mbenford/ngTagsInput/blob/master/src/util.js
 * */
// 'use strict';

function SimplePubSub() {

  var events = {};
  return {
    on: function(names, handler) {
      names.split(' ').forEach(function(name) {
        if (!events[name]) {
          events[name] = [];
        }
        events[name].push(handler);
      });
      return this;
    },
    trigger: function(name, args) {
      angular.forEach(events[name], function(handler) {
        handler.call(null, args);
      });
      return this;
    }
  };
}

angular.module('tabSlideBox', [])
  .directive('onFinishRender', function($timeout) {
    return {
      restrict: 'A',
      link: function(scope, element, attr) {
        if (scope.$last === true) {
          $timeout(function() {
            scope.$emit('ngRepeatFinished');
          });
        }
      }
    };
  })
  .directive('tabSlideBox', ['$timeout', '$window', '$ionicSlideBoxDelegate', '$ionicScrollDelegate',
    function($timeout, $window, $ionicSlideBoxDelegate, $ionicScrollDelegate) {
      // 'use strict';

      return {
        restrict: 'A, E, C',
        link: function(scope, element, attrs, ngModel) {

          var ta = element[0],
            $ta = element;
          $ta.addClass("tabbed-slidebox");
          if (attrs.tabsPosition === "bottom") {
            $ta.addClass("btm");
          }

          //Handle multiple slide/scroll boxes
          var handle = ta.querySelector('.slider').getAttribute('delegate-handle');

          var ionicSlideBoxDelegate = $ionicSlideBoxDelegate;
          if (handle) {
            ionicSlideBoxDelegate = ionicSlideBoxDelegate.$getByHandle(handle);
          }

          var ionicScrollDelegate = $ionicScrollDelegate;
          if (handle) {
            ionicScrollDelegate = ionicScrollDelegate.$getByHandle(handle);
          }

          function renderScrollableTabs() {
            var initialIndex = attrs.tab;
            if (initialIndex == '0') {
              setPosition(0);
            }
            $timeout(function() {
              ionicSlideBoxDelegate.slide(initialIndex);
            }, 0);
          }
          function setPosition(index) {
            scope.slideChanged(index);
            //var topMenuClasses = JSON.parse(window.localStorage.topMenuClasses);
           // scope.getTopMenuClassDetail(topMenuClasses[index]);
          }

          function getX(matrix) {
            matrix = matrix.replace("translate3d(", "");
            matrix = matrix.replace("translate(", "");
            return (parseInt(matrix));
          }
          var events = scope.events;
          events.on('slideChange', function(data) {
            setPosition(data.index);
          });
          events.on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
            renderScrollableTabs();
          });

          renderScrollableTabs();
        },
        controller: function($scope, $attrs, $element, $ionicSlideBoxDelegate) {
          $scope.events = new SimplePubSub();
          $scope.currentSlide = 0;
          $scope.slideHasChanged = function(index) {
            if(index>=0){
              $timeout(function(){
                $scope.currentSlide = $ionicSlideBoxDelegate.currentIndex();
                $scope.events.trigger("slideChange", {
                  "index": index
                });
                $timeout(function() {
                  if ($scope.onSlideMove)
                    $scope.onSlideMove({
                      "index": JSON.parse(index)
                    });
                }, 100);
              },100);
            }
          };

          $scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
            $scope.events.trigger("ngRepeatFinished", {
              "event": ngRepeatFinishedEvent
            });
          });
        }
      };

    }
  ]);
